<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class ContractsController extends AppController {

    public $name = "Contracts";

    public function view($id = null) {
        $this->Contract->id = $id;
        if (!$this->Contract->exists()) {
            throw new NotFoundException(__('Invalid contract'));
        }
        $this->set('contract', $this->Contract->read(null, $id));
    }

    function index($user_id=null) {
        if(is_null($user_id)){
           $user_id =$this->Auth->user('id');
        }
        $this->Session->write('contract_user', $user_id);
        $this->set('user_id', $user_id);
        $this->set('user_nombre', $this->Auth->user('nombre') . " " . $this->Auth->user('primer_apellido'));

        $this->Contract->recursive = -1;
        $this->set('contratos', $this->Contract->find('all', array('conditions' => array('Contract.user_id' => $user_id))));
    }

    function index_user() {
        $this->Session->write('contract_user',$this->Auth->user('id'));
         $this->set('user_id', $this->Auth->user('id'));
        $this->set('user_id', $this->Auth->user('id'));
        $this->set('user_nombre', $this->Auth->user('nombre') . " " . $this->Auth->user('primer_apellido'));

        $this->Contract->recursive = -1;
        $this->set('contratos', $this->Contract->find('all', array('conditions' => array('Contract.user_id' => $this->Auth->user('id')))));
    }

    function add($user_id) {
        
        if (!empty($this->data)) {
            if ($this->Contract->save($this->data)) {
                $this->Session->setFlash('Datos guardados exitosamente', 'flash_custom');
                $this->redirect(array('controller' => 'Contracts', 'action' => 'index', $this->data['Contract']['user_id']));
            }
        } else {
            $this->data['Contract']['user_id']=$user_id; 
            $this->set('user_id', $user_id);
        }
    }

    function edit($id) {
        if (empty($this->data)) {
            $this->Contract->recursive = -1;
            $this->data = $this->Contract->find('first', array('recursive'=>-1, 'conditions' => array('Contract.id' => $id)));
        } else {
            if ($this->Contract->save($this->data)) {
                $this->Session->setFlash('Datos editados exitosamente', 'flash_custom');
                $this->redirect(array('controller' => 'Contracts', 'action' => 'index', $this->data['Contract']['user_id']));
            }
        }
    }

}

?>
